<!-- 分配角色编辑列表 -->
<template>
  <div class=''>
    <el-dialog :model-value="props.allocationVisable" title="分配角色">
      <el-form :model="allocationFormData">
        <el-form-item label="角色名称">
          <el-input v-model="allocationFormData.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="allocationFormData.description" type="textarea" />
        </el-form-item>
        <el-form-item label="是否启用">
          <el-radio-group v-model="allocationFormData.status">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="emits('closeAllocationVisable')">取消</el-button>
          <el-button type="primary" @click="submit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from "vue"
const props = defineProps(["allocationVisable", "itemInfo"])
import * as role from "@/request/role";
import { ElMessage } from "element-plus";
const emits = defineEmits()
const allocationFormData = ref({
  adminCount: undefined,
  createTime: "",
  description: "",
  id: undefined,
  name: "",
  sort: undefined,
  status: 1
})
watch(() => props.itemInfo.id, () => {
  if (props.itemInfo.id !== 0) {
    allocationFormData.value.adminCount = props.itemInfo.adminCount;
    allocationFormData.value.createTime = props.itemInfo.createTime;
    allocationFormData.value.description = props.itemInfo.description;
    allocationFormData.value.id = props.itemInfo.id;
    allocationFormData.value.name = props.itemInfo.name;
    allocationFormData.value.sort = props.itemInfo.sort;
    allocationFormData.value.status = props.itemInfo.status;
  } else {
    allocationFormData.value.adminCount = undefined;
    allocationFormData.value.createTime = '';
    allocationFormData.value.description = '';
    allocationFormData.value.id = undefined;
    allocationFormData.value.name = '';
    allocationFormData.value.sort = undefined;
    allocationFormData.value.status = 1;
  }
})
const submit = () => {
  if (props.itemInfo.id == 0) {
    role.roleCreate(allocationFormData.value).then(res => {
      if (res.code == 200) {
        ElMessage.success("添加成功")
        emits('closeAllocationVisable')
        emits("getAdminList")
      }
    })
  } else {
    role.allocationById(allocationFormData.value).then(res => {
      if (res.code === 200) {
        ElMessage.success("编辑成功")
      } else {
        ElMessage.error(res.message);
      }
    }).finally(() => {
      emits('closeAllocationVisable')
      emits("getAdminList")
    })
  }

}
</script>

<style lang="less" scoped></style>